package com.shortvedio.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shortvedio.base.mapper.SUserInfoMapper;
import com.shortvedio.base.mapper.SUsersLoginMapper;
import com.shortvedio.user.model.dto.ChangePassword;
import com.shortvedio.user.model.dto.SUserInfoDto;
import com.shortvedio.user.model.dto.SUserLoginDto;
import com.shortvedio.user.service.UserService;
import com.shortvedio.base.model.Result;
import com.shortvedio.base.model.po.SUserInfo;
import com.shortvedio.base.model.po.SUsersLogin;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private SUsersLoginMapper sUsersLoginMapper;

    @Autowired
    private SUserInfoMapper sUserInfoMapper;

    @Transactional   //多表操作开启事务
    @Override
    public Result creatUser(SUserLoginDto sUserLoginDto) {
        //信息切换使用po类
        SUsersLogin sUsersLogin = new SUsersLogin();
        BeanUtils.copyProperties(sUserLoginDto,sUsersLogin);
        //查询用户信息是否存在
        LambdaQueryWrapper<SUsersLogin> sUsersLoginLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sUsersLoginLambdaQueryWrapper.eq(SUsersLogin::getEmail,sUsersLogin.getEmail()).or().eq(SUsersLogin::getUserName,sUsersLogin.getUserName());
        //从数据库中查找是否存在
        SUsersLogin sUsersLoginDb = sUsersLoginMapper.selectOne(sUsersLoginLambdaQueryWrapper);
        if (sUsersLoginDb!=null){
            return Result.error("邮箱已经绑定请使用其他邮箱申请账号或用户已经存在");
        }
        //对密码加密
        //使用bCryptPasswordEncoder进行加密
        String password = DigestUtils.md5DigestAsHex(sUserLoginDto.getPassword().getBytes());
        sUsersLogin.setPassword(password);
        sUsersLogin.setCreatedTime(LocalDateTime.now());
        sUsersLogin.setUpdateTime(LocalDateTime.now());
        //创建用户账号
        int insertLogin = sUsersLoginMapper.insert(sUsersLogin);
        if (insertLogin<=0){
            return Result.error("用户账号创建失败，请重新尝试");
        }
        //插入用户信息表  设置默认信息
        SUserInfo sUserInfo = new SUserInfo();
        sUserInfo.setUserId(sUsersLogin.getId());
        sUserInfo.setAge(18);
        sUserInfo.setDescription("没写请尽快填写");
        sUserInfo.setCity("无");
        sUserInfo.setName("无");
        sUserInfo.setUniversity("无");
        int insertInfo = sUserInfoMapper.insert(sUserInfo);
        if (insertInfo<=0){
            return Result.error("用户信息添加失败，账号创建失败请重新创建账号");
        }
        return Result.success("账号创建成功");
    }

    @Override
    public Result changeUserInfo(SUserInfoDto sUserInfoDto,Long userIdToken) {
        //通过userId来获取用户信息表--防止其他用户错误修改
        LambdaQueryWrapper<SUserInfo> sUserInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sUserInfoLambdaQueryWrapper.eq(SUserInfo::getUserId,userIdToken);
        SUserInfo sUserInfo = sUserInfoMapper.selectOne(sUserInfoLambdaQueryWrapper);
        //判断
        if (sUserInfo==null){
            return Result.error("用户信息不存在请联系管理员查看");
        }
        log.info("info:{}",sUserInfo);
        //将更新信息替换到sUserInfo中
        BeanUtils.copyProperties(sUserInfoDto,sUserInfo);
        log.info("sUserInfoDto：{}",sUserInfoDto);
        log.info("info:{}",sUserInfo);
        int update = sUserInfoMapper.updateById(sUserInfo);
        if (update<=0){
            return Result.error("用户信息更新失败");
        }
        return Result.success("用户信息更新成功");
    }

    @Override
    public Result updatePassword(ChangePassword changePassword,Long userId) {
        //获取用户的账户
        SUsersLogin sUsersLogin = sUsersLoginMapper.selectById(userId);
        //获取数据库和输入的密码
        String password = sUsersLogin.getPassword();
        String oldPassword = changePassword.getOldPassword();
        String encodeOld = DigestUtils.md5DigestAsHex(password.getBytes());
        //判断输入的老密码是否与数据库中的相同
        if (!encodeOld.equals(oldPassword)){
            return Result.error("用户密码错误");
        }
        //加密新密码
        String newPassword = DigestUtils.md5DigestAsHex(changePassword.getNewPassword().getBytes());
        sUsersLogin.setPassword(newPassword);
        //更新
        int update = sUsersLoginMapper.updateById(sUsersLogin);
        if (update<=0){
            return Result.error("用户密码更新失败");
        }
        return Result.success("用户密码更新成功");
    }


}
